package com.vinyl.components 
{	
	import fl.core.UIComponent;
	import flash.events.*;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	[IconFile ("../../../../components/09_simpleVSlider/assets/simpleVSlider.png")]
	public class SimpleVSlider extends UIComponent
	{	
		//슬라이더 길이
		private var _way: Number;
		//값 0 ~ 100;
		private var _value: Number;
		//수평슬라이더 & 수직슬라이더
		private var _direction: String;
		
		//슬라이더 pre coumpute 임시 값 
		private var _bottom: Number;
		private var _ty: Number;
		private var _wx: Number;
		private var _wy: Number;
		private var _dragOffset: Number;
		
		//
		public var knob_top: MovieClip;
		public var knob_middle: MovieClip;
		public var knob_bottom: MovieClip;
		
		[Inspectable( type=Number, defaultValue=100 )]
		public function set way( $value ): void {
			var oHeight = this.height;
			var mLen = knob_top.height + knob_middle.height + knob_bottom.height - oHeight;
			_way = $value - mLen;
			init();
		}
		public function get way(): Number {
			return _way;
		}
		
		[Inspectable( type=List, enumeration="horizontal,vertical", defaultValue="vertical" ) ]
		public function set direction(value:String): void {
			_direction = value;
		}
		public function get direction(): String {
			return _direction;
		}
		
		public function set value( $value ): void {
			if( $value > 100 ) $value = 100;
			setValue( $value );
		}
		public function get value(): Number {
			return _value;
		}
		
		/**
		 * constructor
		 * @param Slider ( Void )
		 */
		public function SimpleVSlider( $x = null, $y = null )
		{
			super();
			addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler);
			addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler);
			_value = 0;
			_way = 100;
			x = _wx = $x == null ? x : $x;
			y = _wy = $y == null ? y : $y;
			
			init();
			knobSize( 12 );
		}
		
		override protected function configUI():void
		{
			super.configUI();
		}		
		
		override protected function draw():void
		{
			super.draw();
		}
		
		private function init(): void
		{
			if( direction == "horizontal" )
			{
				_way = _way - this.width;
				_value = value;
				_ty = _wx;
				_bottom = _ty + _way;
			}
			else
			{
				_way = _way - this.height;
				_value = value;
				_ty = _wy;
				_bottom = _ty + _way;
			}
			
			value = _value;
		}
		
		private function setValue( $value: Number ): void
		{
			if( direction == "horizontal" )
			{
				_value = $value;
				x =  Math.round( _bottom - ( 100 - _value ) * _way / 100 );
			}
			else
			{
				_value = $value;
				y = Math.round( _bottom - ( 100 - _value ) * _way / 100 );
			}
			dispatchEvent( new Event( 'onChanged' ) );
		}
		
		private function mouseDownHandler(event:MouseEvent): void 
		{
			_dragOffset = direction == "horizontal" ? event.target.parent.mouseX : event.target.parent.mouseY;
			//-- start listening
			stage.addEventListener( MouseEvent.MOUSE_MOVE, mouseMoveHandler );
			stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
		}
		
		private function mouseUpHandler(event:MouseEvent): void 
		{
			//-- stop listening
			stage.removeEventListener( MouseEvent.MOUSE_MOVE, mouseMoveHandler );
			stage.removeEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
			draging();
			dispatchEvent( new Event( 'onUp' ) );
		}
		
		private function mouseMoveHandler(event:MouseEvent): void 
		{
			draging();
			event.updateAfterEvent();
		}
		
		private function draging(): void
		{
			if( !_dragOffset ) _dragOffset = knob_middle.width / 2 ;
			var pos: Number = ( direction == "horizontal" ? this.parent.mouseX - _wx : this.parent.mouseY - _wy ) - _dragOffset;
				
			if( pos < 0 ) pos = 0;
			if( pos > way ) pos = way;
			
			if( direction == "horizontal" )
			{	
				y = _wy;
				x = _wx + pos;
			}
			else
			{
				x = _wx;
				y = _wy + pos;
			}
			_value = pos * 100 / way;
			dispatchEvent( new Event( 'onChanged' ) );
		}
		
		public function knobSize( $val ): void
		{
			var mLen;
			var oHeight = this.height;
			
			while( numChildren )
				removeChildAt( 0 );
			
			knob_top = new SimpleSlider_vertical_top();
			knob_middle = new SimpleSlider_vertical_middle();
			knob_bottom = new SimpleSlider_vertical_bottom();
			
			knob_top.gotoAndStop(1);
			knob_middle.gotoAndStop(1);
			knob_bottom.gotoAndStop(1);
			
			addChild( knob_top );
			addChild( knob_middle );
			addChild( knob_bottom );
			
			var h =  $val - knob_bottom.height*2;
			knob_middle.height = h;
			knob_middle.y = knob_top.y + knob_top.height;
			knob_bottom.y = knob_middle.y + knob_middle.height;
			
			mLen = knob_top.height + knob_middle.height + knob_bottom.height - oHeight;
			_way = _way - mLen;
			
			_bottom = _ty + way;
			value = _value;
		}
	}
}






















